package com.li.shop.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.li.shop.domain.Coupon;
import com.li.shop.domain.CouponExample;
import com.li.shop.mapper.CouponMapper;
import com.li.shop.service.CouponService;
import com.li.shop.utils.SearchCouponRequest;
import com.li.shop.utils.SearchCouponResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    private CouponMapper couponMapper;

   
    //王志文
    @Override
    //public List<Coupon> listCoupon(Integer pageSize,Integer pageNum) {
    public SearchCouponResponse listCoupon(Integer pageSize, Integer pageNum, String keyword) {
        //PageHelper.startPage(1,10);
        PageHelper.startPage(pageNum,pageSize);
        CouponExample example =new CouponExample();
        if (StringUtils.isNotBlank(keyword)) {
            example.createCriteria().andContentLike("%"+keyword+"%");
        }
        List<Coupon> coupons= couponMapper.selectByExample(example);
        return  new SearchCouponResponse(((Page)coupons).getTotal(),coupons);
        //return CouponMapper.selectByExample(new CouponExample());//分页后返回的page对象，page中有一个list对象
    }
    @Override
    public int deleteCouponByPrimaryKey(String id) {
        return couponMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Coupon findCouponByPrimaryKey(String id) {
        return couponMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Coupon> searchCoupons(SearchCouponRequest request) {
        CouponExample ex = new CouponExample();
//        ex.createCriteria().andCouponIdEqualTo(request.getId());
//        ex.or().andLastNameLike("%" + request.getName() + "%");
        return couponMapper.selectByExample(ex);
    }

    @Override
    public int saveCoupon( Coupon coupon) {
        System.out.println(coupon.getuId()+"couponid");
        if(StringUtils.isNotBlank( coupon.getuId()) ){
            //if( coupon.getuId()!=null ){
            //把domain类中的uId类型转化为String
            return couponMapper.updateByPrimaryKey(coupon);
        } else {
            return couponMapper.insert(coupon);
        }
    }

    /*根据u_id拿到所有的优惠券*/
    @Override
    public List<Coupon> find(String uid) {
        return couponMapper.selectByUid(uid);
    }

}
